package com.searchdemo.thread;

import java.util.concurrent.CountDownLatch;

public class Processor extends Thread {
	
	public Processor() {

	}
	
	CountDownLatch threadsSignal;
	
	public Processor(CountDownLatch threadsSignal) {
		this.threadsSignal = threadsSignal;
	}
	
	@Override
	public void run() {
		if(threadsSignal!=null){
			System.out.println(Thread.currentThread().getName()+" start");
			long start = System.currentTimeMillis();
			dosometing();
			long end = System.currentTimeMillis();
			threadsSignal.countDown();//线程结束时计数器减1  
			System.out.println(Thread.currentThread().getName() + "结束. "+(end-start)+"ms 还有" + threadsSignal.getCount() + " 个线程");  
		}
	}
	
	private void dosometing(){
		long start = System.currentTimeMillis();
		long end = (long) (start+1000*(Math.random()*20));
		System.out.println(Thread.currentThread().getName() + "将耗时"+(end-start)+"ms 还有");
		while(true){
			if(end == System.currentTimeMillis()){
				break;
			}
		}
	}

}
